import java.io.IOException;
import java.sql.*;
import java.util.*;


/**
 * Database
 * The interface with the mysql database
 *
 *@author Zach Lee
 *
 *
 * @version $Revision: 1.0 $
 */

public class Database {
	
	/**
	 * Field conn.
	 */
	private Connection conn;
	/**
	 * Field stm.
	 */
	private Statement stm;
	
	/**
	 * Field userColumns.
	 */
	private final int userColumns = 8;
	/**
	 * Field treatmentColumns.
	 */
	private final int treatmentColumns = 16;
	/**
	 * Field apptColumns.
	 */
	private final int apptColumns = 5;
	
	/**
	 * Open Connection with database
	 * 
	 * @param username The username to access the database
	 * @param password The password to access the database
	 * @param dbms The database management system
	 * @param serverName The name of the server
	 * @param portNumber The port number
	 * @param dbName The name of the database
	 * @throws SQLException  * @throws InstantiationException  * @throws IllegalAccessException  * @throws ClassNotFoundException  */
	public Database(String username, String password, String dbms, String serverName, String portNumber, String dbName) 
			throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
		
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		
	    Properties connectionProps = new Properties();
	    connectionProps.put("user", username);
	    connectionProps.put("password", password);
	    
	    if (dbms.equals("mysql")) {
	    	conn = DriverManager.getConnection("jdbc:" + dbms + "://" + serverName + ":" + portNumber + "/" + dbName, connectionProps);
	    } else if (dbms.equals("derby")) {
	    	conn = DriverManager.getConnection("jdbc:" + dbms + ":" + dbName + ";create=true", connectionProps);
	    }
	    
	    this.stm = conn.createStatement();
	    
	}
	
	/**
	 * 
	 * @param ID The ID of the current user passed in as a string. Should be provided by user via GUI
	 * @param password The password of the user passed in as a string. Should be provided by user via GUI
	 * @return -1 if an error occurred, or 0-3 depending on what level access they have, or return -4 if the account is suspended.	 * @throws SQLException * @throws ClassNotFoundException
	 * 
	 * The call to this method needs to be in the login GUI. The GUI does not need to say that the username or password are specifically 
	 * incorrect because that could be a hindrance to security. */
	public int checkValidUser(String ID, String password) throws SQLException{
		ResultSet result = stm.executeQuery("SELECT Passwd, UserType FROM Users WHERE PatientID = '" + ID + "';");
		if(result.next()){
			if(password.equals(result.getString(1))){
				return result.getInt(2);
			}
		}
		return -1;
	}
	
	/**
	 * Suspends the given user
	 * @param ID The username of the user to suspend
	 * @throws SQLException */
	public void suspendUser(String ID) throws SQLException{
		
		stm.executeUpdate("UPDATE Users SET Suspended = 1 WHERE PatientID = '" + ID + "';");
		
	}
	
	/**
	 * Checks if a user is suspendend
	 * @param ID The username of the user to suspend
	 * @return boolean
	 * @throws SQLException */
	public boolean isSuspended(String ID) throws SQLException{
		
		boolean suspended = false;
		
		ResultSet result = stm.executeQuery("SELECT Suspended FROM Users WHERE PatientID = '" + ID + "';");
		
		if(result.next()){
			if(result.getInt(1) == 1){
				suspended = true;
			}
		}
		
		return suspended;
		
	}
	
	/**
	 * Make a call to this method when you are adding a user (need to specify user type)
	 * @param firstName Patient's first name
	 * @param lastName Patient's last name
	 * @param username Patient's username
	 * @param password Patient's password
	 * @param email Patient's email address
	 * @param sex Patient's sex
	 * @param usertype int
	 * @throws SQLException * @throws UsernameAlreadyTakenException  */
	public void addNewUser(String firstName, String lastName, String username, String password, String email, String sex, int usertype) throws SQLException, UsernameAlreadyTakenException{
		
		ResultSet result = stm.executeQuery("SELECT patientID FROM Users WHERE Users.PatientID = '" + username + "';");
		
		if(!result.next()){
			stm.executeUpdate("INSERT INTO Users VALUES ('" + firstName + "','" + lastName + "','" + username + "','" 
					+ password + "','" + email + "','" + sex +"','"+ usertype + "', 0);");
		} else {
			throw new UsernameAlreadyTakenException();
		}
	}
	
	/**
	 * Make a call to this method when you know for a fact that the user being added is going to be a patient.
	 * @param firstName Patient's first name
	 * @param lastName Patient's last name
	 * @param username Patient's username
	 * @param password Patient's password
	 * @param email Patient's email address
	 * @param sex Patient's sex
	 * @throws SQLException * @throws UsernameAlreadyTakenException  */
	public void addNewPatient(String firstName, String lastName, String username, String password, String email, String sex)
			throws SQLException, UsernameAlreadyTakenException{
		
		addNewUser(firstName, lastName, username, password, email, sex, 3);
		addPatientInformation(firstName, lastName, username, email, sex, "01 January 2011", "", "", "", "");
		
	}
	
	/**
	 * 
	 * @param pharm The Pharmacy that the patient uses. Provided by patient via GUI
	 * @param allergies Any allergies that the patient has. Provided by patient via GUI
	 * @param firstname Patient's first name
	 * @param lastname Patients last name
	 * @param username Patient's username
	 * @param email Patient's email
	 * @param sex Patient's sex
	 * @param dob Drop down box based GUI in format (YYYY Month (spelled out) DD (leading zero))
	 * @param insurance Patients insurance
	 * @param phone Patient's phone number
	 * @throws SQLException 
	 * 
	 */
	public void updatePatientInformation(String firstname, String lastname, String username, String email, String sex, 
			String dob, String pharm, String insurance, String allergies, String phone) throws SQLException{

		ResultSet result = stm.executeQuery("SELECT patientID FROM patientInfo WHERE patientInfo.PatientID = '" + username + "';");
		if(result.next()) {
			stm.executeUpdate("UPDATE patientInfo SET FirstName = '" + firstname + "', LastName = '" + lastname + "', Email = '" + email + "', Pharmacy = '" + pharm + 
					"', InsuranceCompany = '"+insurance+"', Allergies = '" + allergies + "', phonenumber = '" + phone + "' WHERE patientId = '" + username + "';");
			stm.executeUpdate("UPDATE Users SET FirstName = '" + firstname + "', LastName = '" + lastname + "', Email = '" + email + "' WHERE patientId = '" + username + "';");
		}
		
	}
	
	/**
	 * Adds patient information based on what is passed in
	 * @param firstname Patient's firstname
	 * @param lastname Patient's lastname
	 * @param username Patient's username
	 * @param email Patient's email
	 * @param sex Patient's sex
	 * @param dob Patient's date of birth
	 * @param pharm Patient's pharmacy
	 * @param insurance Patient's insurance carrier
	 * @param allergies Patient's allergies
	 * @param phone Patient's phone number
	 * @throws SQLException */
	public void addPatientInformation(String firstname, String lastname, String username, String email, String sex, String dob, 
			String pharm, String insurance, String allergies, String phone) throws SQLException{

		ResultSet result = stm.executeQuery("SELECT patientID FROM patientInfo WHERE patientInfo.PatientID = '" + username + "';");
		if(!result.next()) {
			stm.executeUpdate("INSERT INTO patientInfo VALUES ('" + firstname + "','" + lastname + "','" + username + "','" + email + "','" + sex +
					"','" + dob + "','" + pharm + "','" + insurance + "','" + allergies + "', phonenumber = '" + phone + "');");
		}
		
	}
	
	
	/**
	 * Adds an appointment to the database
	 * @param date ENSURE IT IS IN THE FORMAT OF YYYY MONTH DD (2011 April 12) or (1915 September 02)
	 * @param time ENSURE 24 HOUR TIME (0800 or 1900..)
	 * @param reason The patient will input this into a GUI
	 * @param doctor This will be drop down boxified.
	 * @param patientID String
	 * @throws SQLException * @throws SecurityException  * @throws IOException  */
	public void addAppointment(String patientID, String date, String time, String reason, String doctor) throws SQLException, SecurityException{
		
		stm.executeUpdate("INSERT INTO appointment VALUES ('" + date + "','" + time + "','" + patientID + "','" + reason + "','" + doctor + "');");
	}
	
	/**
	 * Gets all of the treatment records for the patient passed in
	 * @param ID
	 * @return String[][]
	 * @throws SQLException */
	public String[][] requestMedicalHistory(String ID) throws SQLException{
		
		int columns = 16;
		ArrayList<String[]> allRecords = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM treatmentRecord WHERE patientID = '" + ID + "';");
		while(result.next()){
			String[] record = new String[columns];
			for(int i = 0; i <= columns - 1; i++){
				record[i] = result.getString(i+1);
			}
			
			allRecords.add(record);
			
		}
		
		String[][] allRecordsArray = new String[userColumns][1];

		return allRecords.toArray(allRecordsArray);
		
	}
	
	
	/**
	 * Gets all of the treatment records in the database
	 * @return String[][]
	 * @throws SQLException */
	public String[][] requestMedicalHistory() throws SQLException{
		ArrayList<String[]> allRecords = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM treatmentRecord;");
		while(result.next()){
			String[] record = new String[treatmentColumns];
			for(int i = 0; i <= treatmentColumns - 1; i++){ 
				record[i] = result.getString(i+1);
			}
			
			allRecords.add(record);
			
		}
		
		String[][] allRecordsArray = new String[userColumns][1];

		return allRecords.toArray(allRecordsArray);
		
	}
	
	/**
	 * Adds a treatment record to the treatmentRecord table with blank values
	 * @param date String Date in "DD Month YYYY" format
	 * @param time String time in tttt (e.g. 1300) format
	 * @throws SQLException * @throws SecurityException  * @throws IOException  */
	public void addTreatmentRecord(String date, String time) throws SQLException, SecurityException{
		
		stm.executeUpdate("INSERT INTO treatmentRecord VALUES ('','','" + date +"','" + time + "','','','','','',0,0,0,0,0,0,'');");
		
	}
	
	/**
	 * Updates the basic info for a treatment record
	 * @param patientID Patient's username
	 * @param doctor Doctor of the treatment record
	 * @param nurse Nurse on the treatment record
	 * @param complaint Cheif complaint of the patient
	 * @param diagnosis Patient's diagnosis
	 * @param date String Date in "DD Month YYYY" format
	 * @param time String time in tttt (e.g. 1300) format
	 * @throws SQLException * @throws SecurityException * @throws IOException */
	public void updateTR_basicInfo(String date, String time, String patientID, String doctor, String nurse, String complaint, String diagnosis) 
			throws SQLException, SecurityException{
		
		stm.executeUpdate("UPDATE treatmentRecord SET Doctor = '" + doctor + "', Nurse = '" + nurse 
					+ "', PatientID = '" + patientID + "', ChiefComplaint = '" + complaint 
					+ "', Diagnosis = '" + diagnosis +"' WHERE D = '" + date + "' AND T = '" + time + "';");
		
	
	}
	
	/**
	 * Update the vital signs for the given treatment record
	 * @param weight Patient's weight 
	 * @param height Patient's height
	 * @param temp Patient's temperature
	 * @param pulse Patient's pulse
	 * @param diastolic Patient's diastolic blood pressure
	 * @param systolic Patients systolic blood pressure
	 * @param date String Date in "DD Month YYYY" format
	 * @param time String time in tttt (e.g. 1300) format
	 * @throws SQLException * @throws SecurityException * @throws IOException */
	public void updateTR_vitalSigns(String date, String time, float weight, int height, float temp, int pulse, int diastolic, int systolic) 
		throws SQLException, SecurityException{
		
		stm.executeUpdate("UPDATE treatmentRecord SET Weight = " + weight + ", Height = " + height 
			+ ", Temperature = "+ temp +", Pulse = " + pulse + ", BloodPressureD = " + diastolic 
			+ ", BloodPressureS = " + systolic + " WHERE D = '" + date + "' AND T = '" + time + "';");

	}
	
	/**
	 * Updates the doctors orders for the given treatment record
	 * @param drug The drug given to the patient
	 * @param labWork Lab work done on the patient
	 * @param instructions Instructions for the patient
	 * @param date String Date in "DD Month YYYY" format
	 * @param time String time in tttt (e.g. 1300) format
	 * @throws SQLException * @throws SecurityException * @throws IOException */
	public void updateTR_doctorsOrders(String date, String time, String drug, String labWork, String instructions) 
	throws SQLException, SecurityException{
	
		stm.executeUpdate("UPDATE treatmentRecord SET Prescription = '" + drug + "', LabWorkPerformed = '" + labWork 
				+ "', Instructions = '"+ instructions +"' WHERE D = '" + date + "' AND T = '" + time + "';");

	}
	
	
	/**
	 * Gets all of the patients in an ArrayList
	 * @param ID The username of the patient
	 * @return A arraylist of the patients. There are six fields per row so do a for loop and all that awesomeness * @throws SQLException */
	public ArrayList<String> viewPatient(String ID) throws SQLException{
		ArrayList<String> s = new ArrayList<String>();

		ResultSet result = stm.executeQuery("SELECT * FROM Users WHERE usertype = 3 AND patientID = '" + ID + "';");
		while(result.next()){
			for(int i = 1; i <= 7; i++){
				s.add((result.getString(i)));
			}
			
		}
		for(int i = 0; i<s.size();i++){
			System.out.print(s.get(i) + " ");
			if((i%6 == 0) && i != 0){
				System.out.println();
			}
		}
		return s;
	}
	
	/**
	 * Gets all of the user information
	 * @param ID The user name of the user to get the information of
	 * @return A arraylist of the patients. There are six fields per row so do a for loop and all that awesomeness * @throws SQLException */
	public ArrayList<String> viewUser(String ID) throws SQLException{
		ArrayList<String> s = new ArrayList<String>();

		ResultSet result = stm.executeQuery("SELECT * FROM Users WHERE patientID = '" + ID + "';");
		while(result.next()){
			for(int i = 1; i <= userColumns; i++){
				s.add((result.getString(i)));
			}
			
		}
		for(int i = 0; i<s.size();i++){
			System.out.print(s.get(i) + " ");
			if((i%5 == 0) && i != 0){
				System.out.println();
			}
		}
		return s;
	}
	
	/**
	 * This method just prints the Users to the console. It should not be called for anything other than testing.
	 * @throws SQLException */
	public void viewAllUsers() throws SQLException{
		ArrayList<String> s = new ArrayList<String>();

		ResultSet result = stm.executeQuery("SELECT * FROM Users;");
		while(result.next()){
			for(int i = 1; i <= userColumns; i++){
				s.add((result.getString(i)));
			}
			
			s.add("\n\n");
			
		}
		for(int i = 0; i<s.size();i++){
			System.out.print(s.get(i) + " ");

		}
		
		System.out.println();

	}
	
	/**
	 * Gets all of the users in a 2-D array
	 * @return A 2D array corresponding to what a table/model needs to populate with data * @throws SQLException */
	public String[][] getAllUsers() throws SQLException{
		ArrayList<String[]> allUsers = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM Users;");
		while(result.next()){
			String[] user = new String[userColumns];
			for(int i = 0; i <= userColumns - 1; i++){
				user[i] = result.getString(i+1);
			}
			
			allUsers.add(user);
			
		}
		
		String[][] allUserArray = new String[userColumns][1];

		return allUsers.toArray(allUserArray);	

	}
	
	
	/**
	 * Gets all of the PATIENTS in a 2-D array
	 * @return A 2D array corresponding to what a table/model needs to populate with data * @throws SQLException */
	public String[][] getAllPatients() throws SQLException{
		
		int columns = 5; // firstname, lastname, username, email, sex //
		int username = 3, first = 1, last = 2, email = 5, sex = 6;
		
		ArrayList<String[]> allUsers = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM Users WHERE usertype = 3;");
		while(result.next()){
			String[] user = new String[columns];
			user[0] = result.getString(first);
			user[1] = result.getString(last);
			user[2] = result.getString(username);
			user[3] = result.getString(email);
			user[4] = result.getString(sex);
			allUsers.add(user);
			
		}
		
		String[][] allUserArray = new String[columns][1];

		return allUsers.toArray(allUserArray);	

	}
	
	
	/**
	 * Gets all of the appointments in a 2-D array
	 * @return A 2D array corresponding to what a table/model needs to populate with data * @throws SQLException */
	public String[][] getAllAppts() throws SQLException{
		
		int columns = 5; // date, time, patientID, reason, doctor//
		int date = 1, time = 2, patientID = 3, reason = 4, doctor = 5, treatment = 6;
		
		ArrayList<String[]> allAppts = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM appointment;");
		while(result.next()){
			String[] appt = new String[columns];
			appt[0] = result.getString(date);
			appt[1] = result.getString(time);
			appt[2] = result.getString(patientID);
			appt[3] = result.getString(reason);
			appt[4] = result.getString(doctor);
			allAppts.add(appt);
			
		}
		
		String[][] allApptsArray = new String[columns][1];

		return allAppts.toArray(allApptsArray);	

	}
	
	/**
	 * Gets an array of an appointment with value for each column from the appointment database table
	 * @param date The date of the appointment
	 * @param time The time of the appointment
	 * @return A string array representation of the appointment * @throws SQLException */
	public String[] getAppt(String date, String time) throws SQLException{
		String[] appt = new String[5];
		
		ResultSet result = stm.executeQuery("SELECT * FROM appointment WHERE D = '" + date + "' AND T ='" + time + "';");
		
		if(result.next()){
			for(int i = 0; i <= apptColumns - 1; i++){
				appt[i] = result.getString(i+1);
			}
		}

		return appt;
	}

	/**
	 * Gets an array of an appointment with value for each column from the appointment database table
	 * @param date The date of the appointment
	 * @param time The time of the appointment
	 * @return A string array representation of the appointment * @throws SQLException */
	public String[] getTreatment(String date, String time) throws SQLException{
		String[] treatment = new String[treatmentColumns];
		
		ResultSet result = stm.executeQuery("SELECT * FROM treatmentRecord WHERE D = '" + date + "' AND T ='" + time + "';");
		
		if(result.next()){
			for(int i = 0; i <= treatmentColumns - 1; i++){
				treatment[i] = result.getString(i+1);
			}
		}
		return treatment;
	}
	
	/**
	 * Gets all of the appointments for a specific patient in a 2-D array
	 * @param username The username of the patient
	 * @return A 2D array corresponding to what a table/model needs to populate with data * @throws SQLException */
	public String[][] getPatientAppts(String username) throws SQLException{
		
		int columns = 4; // date, time, reason, doctor //
		int date = 1, time = 2, reason = 4, doctor = 5;
		
		ArrayList<String[]> allAppts = new ArrayList<String[]>();

		ResultSet result = stm.executeQuery("SELECT * FROM appointment WHERE patientID = '" + username + "';");
		while(result.next()){
			String[] appt = new String[columns];
			appt[0] = result.getString(date);
			appt[1] = result.getString(time);
			appt[2] = result.getString(reason);
			appt[3] = result.getString(doctor);
			allAppts.add(appt);
			
		}
		
		String[][] allApptsArray = new String[columns][1];

		return allAppts.toArray(allApptsArray);	

	}
	
	
	/**
	 * Deletes the user from the database
	 * @param username The user's username
	 * @throws SQLException
	 */
	public void deleteUser(String username) throws SQLException{
		
		stm.executeUpdate("DELETE FROM Users WHERE PatientID = '" + username + "';");

	}
	
	/**
	 * Deletes the appointment from the database
	 * @param date The date of the appointment to delete
	 * @param time The time of the appointment to delete
	 * @throws SQLException
	 */
	public void deleteAppt(String date, String time) throws SQLException{
		
		stm.executeUpdate("DELETE FROM appointment WHERE D = '" + date + "' AND T = '" + time + "';");
		stm.executeUpdate("DELETE FROM treatmentRecord WHERE D = '" + date + "' AND T = '" + time + "';");

	}

	/**
	 * Updates user information
	 * @param username Username of the user
	 * @param firstName first name of the user
	 * @param lastName Last name of the user
	 * @param email User's email address
	 * @param sex The sex of the user
	 * @param userType The user type of the user
	
	 * @throws SQLException */
	public void updateUser(String username, String firstName, String lastName, String email, String sex, int userType) throws SQLException{

		stm.executeUpdate("UPDATE Users SET FirstName = '" + firstName + "', LastName = '" + lastName + "', Email = '" 
				+ email + "', Sex = '" + sex + "', UserType = " + userType + " WHERE patientID = '" + username + "';");
		
		if(userType == 3){
			stm.executeUpdate("UPDATE patientInfo SET FirstName = '" + firstName + "', LastName = '" + lastName + "', Email = '"
					+ email + "' WHERE patientId = '" + username + "';");
		}
		
			
		
	}
	
	/**
	 * Updates user information (More detailed)
	 * @param sex Sex The sex of the user
	 * @param suspended Whether the user is currently suspended
	 * @param username String The username of the user
	 * @param firstname String The firstname of the user
	 * @param lastname String The last name of the user
	 * @param password String The password of the user
	 * @param email String The email of the user
	 * @param usertype String The usertype of the user
	 * @throws SQLException */
	public void updateUser(String username, String firstname, String lastname, String password, 
			String email, String sex, String usertype, int suspended) throws SQLException{

			stm.executeUpdate("UPDATE Users SET FirstName = '" + firstname + "', LastName = '" + lastname + 
					"', Passwd = '" + password + "', Email = '" + email + "', Sex = '" + sex + "', UserType = '"
					+ usertype +"', Suspended = '" + suspended + "' WHERE patientID = '" + username + "';");
			
			if(usertype.equals("3")){
				stm.executeUpdate("UPDATE patientInfo SET FirstName = '" + firstname + "', LastName = '" + lastname + "', Email = '"
						+ email + "' WHERE patientId = '" + username + "';");
			}
		
	}
	
	
	/**
	 * This gives all appointments on a certain day.
	 * @param date Pass in the date in format YYYY Month DD
	 * @return an arraylist of all the appointments where an appointment is separated by a newline * @throws SQLException */
	public ArrayList<String> viewAppointments(String date) throws SQLException{ 
		ArrayList<String> s = new ArrayList<String>();

		ResultSet result = stm.executeQuery("SELECT * FROM appointment WHERE D = '" + date +"';");
		while(result.next()){
			for(int i = 1; i <= 4; i++){
				s.add((result.getString(i)));
			}
			s.add("\n");
			
			
		}
		for(int i = 0; i<s.size();i++){
			System.out.print(s.get(i) + " ");
		}
		return s;
		
	}
	
	/**
	 * Will print our the current month's income statement
	 * @param month Provide a month. Not the actual date but the month. If you want to supply the date, you can, but follow the typical YYYY Month DD format.
	 * @return An arraylist, separated per row by a new line, of all of the reports in provided month * @throws SQLException */
	public ArrayList<Integer> printIncomeStatement(String month) throws SQLException{
		ArrayList<Integer>  s = new ArrayList<Integer> ();

		ResultSet result = null;
		try {
			result = stm.executeQuery("SELECT Prescription, LabWorkPerformed FROM treatmentRecord WHERE D LIKE '%" + month +"%';");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = 0;
		int presCount = 0;
		int lwCount = 0;
		while(result.next()){

			if(!result.getString(1).equals("None")){
				presCount++;
			}
			if(!result.getString(2).equals("None")){
				lwCount++;
			}
			i++;
		}
			s.add(presCount);
			s.add(lwCount);


		s.add(i);

		return s;
		
	}
	
	/**
	 * This bad boy provides an arraylist with all the information necessary for the patient invoice. I broke it up into two different queries because it was like 4:00 am and my
	 * mind could not figure out how to do this as a single query. Anyway, inside the array list is the First and Last name of the patient, the doctor who saw the patient, the 
	 * prescription given to the patient and the labworkperformed. 
	 * @param ID The patientID 
	 * @return An arraylist filled with all sorts of goodies. Read the javadoc * @throws SQLException */
	public ArrayList<String> patientInvoice(String ID) throws SQLException{
		ArrayList<String> s = new ArrayList<String>();

		ResultSet result1 = null;
		ResultSet result2 = null;
		try {
			result1 = stm.executeQuery("SELECT FirstName, LastName " +
					"FROM Users WHERE Users.patientID = '" + ID + "';");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if(result1.next()){
			s.add(result1.getString(1));
			s.add(result1.getString(2));
		}
		
		try {
			result2 = stm.executeQuery("SELECT D, doctor, Prescription, LabWorkPerformed " +
					"FROM Users, treatmentRecord WHERE treatmentRecord.patientID = '" + ID + "';");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(result2.next()){
			s.add(result2.getString(1));
			s.add(result2.getString(2));
			s.add(result2.getString(3));
			s.add(result2.getString(4));
		}
		for(int i = 0; i<s.size();i++){
			System.out.print(s.get(i) + " ");
		}
		return s;
		
	}
	
	
	/**
	 * This is a call to get patient information to update
	 * @param ID The username of the patient whos information you want
	 * @return A arraylist of the patient info. * @throws SQLException */
	public ArrayList<String> getPatientInfo(String ID) throws SQLException{
		ArrayList<String> s = new ArrayList<String>();
		int columns = 10;

		ResultSet result = stm.executeQuery("SELECT * FROM patientInfo WHERE patientID = '" + ID + "';");
		while(result.next()){
			for(int i = 1; i <= columns; i++){
				s.add((result.getString(i)));
			}
		}

		return s;
	}
	
	
	/**
	 * Gets a string of all the users of a certain type
	 * @param usertype The type of user you want to get
	 * @return String[] A string of users of the type given
	 * @throws SQLException
	 */
	public String[] getUsersOfType(int usertype) throws SQLException{
		ArrayList<String> users = new ArrayList<String>();
		
		ResultSet result = stm.executeQuery("SELECT PatientID FROM Users WHERE UserType = " + usertype + ";");
		while(result.next()){
			users.add(result.getString(1));
		}
		
		String[] userArray = new String[users.size()];
		return users.toArray(userArray);
		
	}
	
	/**
	 * All responsibility for this is removed from the user. It makes its debut in HealthHistoryChart.java. For the curious mind, this 
	 * uses the patientID to get the Weight from the database
	 * @param ID A string of the users patientID
	 * @return A double array of weights * @throws SQLException */
	public double[] getUserWeightForPlot(String ID) throws SQLException{
		ArrayList<Double> weights = new ArrayList<Double>();
		
		ResultSet result = stm.executeQuery("SELECT Weight FROM treatmentRecord WHERE PatientID = '" + ID + "';");
		while(result.next()){
			weights.add(result.getDouble(1));
		}
		
		double[] weightsArray = new double[weights.size()];
		for(int i = 0; i < weights.size(); i++){
			weightsArray[i] = weights.get(i);
		}
		return weightsArray;
		
	}
	
	/**
	 * Similar to getUserWeightForPlot(), I took all responsibility of this call away from the user. It is completely handled inside 
	 * HealthHistoryChart.java. However, if you are curious, this uses the patientID to get a list of the Dates associated 
	 * with that user.
	 * @param ID The patientID of the user
	 * @return A string array of the dates * @throws SQLException */
	public String[] getUserDateForPlot(String ID) throws SQLException{
		ArrayList<String> dates = new ArrayList<String>();
		
		ResultSet result = stm.executeQuery("SELECT D FROM treatmentRecord WHERE PatientID = '" + ID + "';");
		while(result.next()){
			dates.add(result.getString(1));
		}
		
		String[] datesArray = new String[dates.size()];
		return dates.toArray(datesArray);
		
	}
	
	/**
	 * Main method for testing purposes
	 * @param args String[] Command line arguments
	 * @throws SQLException
	 */
	public static void main(String[] args){
		
		String username = "codingquad";
		String password = "ZioVJ57F";
		String dbms = "mysql";
		String dbName = "cs2340_db";
		String serverName = "cs2340.anybeth.com";
		String portNumber = "3306";
		
		Database db;
		
		try {
			System.out.println("Attempting to connect now...");
			db = new Database(username, password, dbms, serverName, portNumber, dbName);
			System.out.println("Connected to database");
			db.viewAllUsers(); //prints out all users so we can know what login credentials to test			
		} catch (Exception e) {
			System.out.println("Error Connecting to Database");
			e.printStackTrace();
		} 
	}
}